package com.gs.douban.gui;

import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import com.gs.douban.worker.LikedSongDownloader;
import com.mpatric.mp3agic.NotSupportedException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JLabel;

import org.apache.commons.httpclient.HttpException;
import javax.swing.JTextArea;

public class MainFrame {

	private JFrame frame;
	private JTextField textField_1;
	private JFileChooser fDialog;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame window = new MainFrame();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainFrame() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 547, 397);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		textField_1 = new JTextField();
		textField_1.setBounds(153, 118, 207, 21);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		final JTextArea txtrFlagokAcBidhgosalnspg = new JTextArea();
		txtrFlagokAcBidhgosalnspg.setLineWrap(true);
		txtrFlagokAcBidhgosalnspg.setBounds(153, 40, 207, 68);
		frame.getContentPane().add(txtrFlagokAcBidhgosalnspg);

		final JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setBounds(153, 209, 207, 15);
		frame.getContentPane().add(lblNewLabel_1);
		
		JButton btnNewButton = new JButton("开始");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Thread t = new Thread(new Runnable() {
					public void run() {
						fDialog = new JFileChooser(); // 文件选择器
						fDialog.setDialogTitle("请选择要导出目录");
						fDialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
						int result = fDialog.showOpenDialog(frame);
						File file = null;
						if (JFileChooser.APPROVE_OPTION == result) {
							file = fDialog.getSelectedFile();
							if (!file.isDirectory()) {
								JOptionPane.showMessageDialog(null, "你选择的目录不存在");
								return;
							}
							String path = file.getAbsolutePath();
							try {
								LikedSongDownloader.down(txtrFlagokAcBidhgosalnspg.getText(), Integer.parseInt(textField_1.getText()), path,lblNewLabel_1);
							} catch (NumberFormatException e) {
								JOptionPane.showMessageDialog(null, "请输入数字");
								e.printStackTrace();
							} catch (HttpException e) {
								e.printStackTrace();
							} catch (IOException e) {
								e.printStackTrace();
							} catch (InterruptedException e) {
								e.printStackTrace();
							} catch (NotSupportedException e) {
								e.printStackTrace();
							} catch(IllegalArgumentException e){
								JOptionPane.showMessageDialog(null, e.getMessage());
							}
							lblNewLabel_1.setText("OK!");
						} else {
							return;
						}
					}
				});
				t.start();
			}
		});
		btnNewButton.setBounds(214, 176, 93, 23);
		frame.getContentPane().add(btnNewButton);
		
		JLabel lblCookie = new JLabel("cookie");
		lblCookie.setBounds(85, 67, 54, 15);
		frame.getContentPane().add(lblCookie);
		
		JLabel lblNewLabel = new JLabel("retry");
		lblNewLabel.setBounds(85, 121, 54, 15);
		frame.getContentPane().add(lblNewLabel);
		
		
		
	}
}
